package com.xindz.hgq.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;
import com.lztx.shared.shared.util.loadsir.callback.EmptyCallback;
import com.lztx.shared.shared.util.loadsir.callback.ErrorCallback;
import com.lztx.shared.shared.util.loadsir.callback.LoadingCallback;
import com.xindz.hgq.base.options.BaseActivityOptions;

/**
 * @name HGQBase
 * @class name：com.xindz.hgq.base
 * @describe 封装了MVP ,LoadSir (LoadSir需要在AppLication 进行公共配置)
 * @anthor XINDZ
 * @time 2018/1/11 10:02
 * @change
 * @chang time
 * @class describe
 */
public abstract class BaseActivity<V extends BaseView, P extends BasePresenter<V>> extends AppCompatActivity {
    /**
     * 页面配置 返回Null 使用默认配置
     *
     * @return
     */
    public abstract BaseActivityOptions setOption();

    private BaseActivityOptions options;

    public BaseActivityOptions getOptions() {
        return options;
    }

    {
        if (setOption() != null) {
            options = setOption();
        } else {
            options = new BaseActivityOptions();
        }

    }

    private P presneter;
    private V view;

    public P getPresneter() {
        return presneter;
    }

    private MvpInterface<V, P> mvpInterface;

    /**
     * LoadSir 需要 AppLication中初始化配置
     * LoadSir在setContentView(int layoutResID) 方法中进行的初始化
     */
    public LoadService loadService;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (options.isOpenMVP()) initMVP();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (this.presneter != null) this.presneter.detachView();
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        //如果开启LaodSir
        if (getOptions().isOpenLoadSir()) {
            loadService = LoadSir.getDefault().register(this, getOptions().getLoadSirListener());
        }
    }

    private void initMVP() {
        if (options.getMvpInterface() == null) {
            throw new NullPointerException("MVP模式需求实现MvpInterface接口");
        }
        this.mvpInterface = options.getMvpInterface();

        if (this.presneter == null) {
            //创建P层
            this.presneter = mvpInterface.createPresneter();
        }

        if (this.view == null) {
            //创建V层
            this.view = mvpInterface.createView();
        }
        //判定是否为空
        if (this.presneter == null) {
            throw new NullPointerException("presneter不能够为空");
        }
        if (this.view == null) {
            throw new NullPointerException("view不能够为空");
        }
        //绑定
        this.presneter.attachView(this.view);
    }


    /**
     * loadService 成功
     */
    public void showLoadSirSuccess() {
        if (loadService != null) loadService.showSuccess();
    }

    /**
     * loadService 异常
     */
    public void showLoadSirError() {
        showLoadSirCallBack(ErrorCallback.class);
    }

    /**
     * loadService 空
     */
    public void showLoadSirEmpty() {
        showLoadSirCallBack(EmptyCallback.class);
    }

    /**
     * loadService 加载中
     */
    public void showLoadSirLoading() {
        showLoadSirCallBack(LoadingCallback.class);
    }

    /**
     * loadService
     */
    public void showLoadSirCallBack(Class<? extends Callback> callback) {
        if (loadService != null) loadService.showCallback(callback);
    }
}
